package com.itheima.reggie.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.SetmealService;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import javafx.scene.input.DataFormat;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.datatransfer.DataFlavor;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<Setmeal> findMy(Integer page, Integer pageSize, String name) {
        //分页
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        //条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //进行查询
        setmealPage = setmealMapper.selectPage(setmealPage, wrapper);

        //遍历
        List<Setmeal> records = setmealPage.getRecords();
        if (CollectionUtil.isNotEmpty(records)){
            for (Setmeal record : records) {
                //查询category
                Category category = categoryMapper.selectById(record.getCategoryId());
                record.setCategoryName(category.getName());

                //查询套餐对应菜品信息
                LambdaQueryWrapper<SetmealDish> setmealDish = new LambdaQueryWrapper<>();
                setmealDish.eq(SetmealDish::getDishId,record.getId());
                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(setmealDish);
                record.setSetmealDishes(setmealDishes);
            }
        }
        return setmealPage;
    }

    @Override
    public List<Dish> findMyId(Long categoryId,String name) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        log.info("nm{}",name);

        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        wrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId)
        .in(Dish::getStatus,1);//写死了

        List<Dish> dishes = dishMapper.selectList(wrapper);
        return dishes;
    }

    @Override
    public void save(Setmeal setmeal) {
        setmealMapper.insert(setmeal);

        //遍历,菜品信息
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)){
            for (SetmealDish setmealDish : setmealDishes) {
                //设置id
                setmealDish.setSetmealId(setmeal.getId());
                //保存
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public void update(Long ids) {
        //判断当前要删除的这一批套餐是否有在售状态
        //select count(0) from setmeal where status = 1 and id in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus,1)
                .in(Setmeal::getId,ids);
        //删除
        int delete = setmealMapper.delete(wrapper);
        if (delete>0){
            throw new CustomException("当前套餐处于在售状态,无法删除");
        }

        // 删除中间表
        //条件
        LambdaQueryWrapper<SetmealDish> w = new LambdaQueryWrapper<>();
        w.in(SetmealDish::getSetmealId,ids);
        //删除
        setmealDishMapper.delete(w);

        // 删除套餐表
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getStatus,0)
                .in(Setmeal::getId,ids);
        setmealMapper.delete(wrapper1);
    }

    @Override
    public Setmeal zhu(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishes);
        return setmeal;

    }

    @Override
    public void updated(Setmeal setmeal) {
        setmealMapper.updateById(setmeal);
        //删除套餐里面的菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //加入修改后的
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil .isNotEmpty(setmealDishes)){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
        }
        }
    }

    @Override
    public void ts(int id, Long[] ids) {
        for (Long aLong : ids) {
            Setmeal setmeal = setmealMapper.selectById(aLong);
            if (id!=0){
                setmeal.setStatus(1);
            }else if (id!=1){
                setmeal.setStatus(0);
            }
            setmealMapper.updateById(setmeal);
        }
    }

    @Override
    public List<Setmeal> findBy(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId)
        .in(Setmeal::getStatus,status);
        List<Setmeal> list = setmealMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(list)){
            for (Setmeal setmeal : list) {
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getDishId,setmeal.getId());
                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishes);
            }
        }
        return list;
    }

    @Override
    public List<Dish> jin(Long id) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getDishId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        //封装菜品信息
        List<Dish> objects = new ArrayList<>();
        //遍历得到的菜品
        for (SetmealDish setmealDish : setmealDishes) {
            Dish dish = dishMapper.selectById(setmealDish.getId());
            //完全不懂
            dish.setCopies(setmealDish.getCopies());
            objects.add(dish);
        }
        return objects;
    }


}
